package com.webdesign.service.impl;

import java.awt.AWTException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.webdesign.dao.mapper.TbDisplyMapper;
import com.webdesign.dao.mapper.TbUserMapper;
import com.webdesign.dao.mapper.TbUserhomeworkMapper;
import com.webdesign.dto.DisplayDTO;
import com.webdesign.entity.TbDisply;
import com.webdesign.entity.TbDisplyExample;
import com.webdesign.entity.TbUser;
import com.webdesign.entity.TbUserhomework;
import com.webdesign.service.ITbDisplyService;
import com.webdesign.util.CutPictureUtil;

@Service("TbDisplyService")
public class TbDisplyService implements ITbDisplyService {

	@Autowired
	private TbDisplyMapper tbDisplyMapper;
	
	@Autowired
	private TbUserhomeworkMapper tbUserhomeworkMapper;
	
	@Autowired
	private TbUserMapper tbUserMapper;
	
	/*
	 * 将作品加入展示作业，包括学生作业编号、评语以及展示类型
	 */
	@Override
	public Boolean addDisplay(String userhomeworkid, String comment,
			String classify) {
		CutPictureUtil cut = new CutPictureUtil();
		TbUserhomework tbUserhomework = tbUserhomeworkMapper.selectByPrimaryKey(userhomeworkid);
		TbDisply tbDisplay = new TbDisply();
		tbDisplay.setUserid(tbUserhomework.getUserid());
		tbDisplay.setUserhomeworkid(tbUserhomework.getUserhomeworkid());
		tbDisplay.setClassify(Byte.parseByte(classify));
		tbDisplay.setComment(comment);
		tbDisplay.setCreatetime(new Date());
		
		String img = null;
		try {
			
			//根据tbuserhomework表中的作品链接，打开网页用网页截屏工具截屏，返回保存的作品图片的链接
			img = cut.haveImage(tbUserhomework.getAddress());
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		tbDisplay.setImgaddr(img);
		
		int flag;
		TbDisply display = tbDisplyMapper.selectByPrimaryKey(userhomeworkid);
		//判断是否已经存在该学生该作业的展示信息，如果已存在的话覆盖更新
		if(display != null){
			flag = tbDisplyMapper.updateByPrimaryKey(tbDisplay);
		}else{
			flag = tbDisplyMapper.insert(tbDisplay);
		}
		
		if(flag > 0){
			return true;
		}else{
			return false;
		}
	}
        
	/*
	 * 
	 * 学生查看展示作业
	 */
	@Override
	public List<DisplayDTO> homeworkDisplay(){
		List<DisplayDTO> displayDTOList = new ArrayList<DisplayDTO>();
		TbDisplyExample tbDisplayExample = new TbDisplyExample();
		tbDisplayExample.createCriteria().getAllCriteria();
		List<TbDisply> tbDisplayList = tbDisplyMapper.selectByExample(tbDisplayExample);
		for(TbDisply tbDisplay : tbDisplayList){
			TbUser tbUser = new TbUser();
			tbUser = tbUserMapper.selectByPrimaryKey(tbDisplay.getUserid());
			TbUserhomework tbUserhomework = new TbUserhomework();
			tbUserhomework = tbUserhomeworkMapper.selectByPrimaryKey(tbDisplay.getUserhomeworkid());
			String address = tbUserhomework.getAddress();
			String homeworkid = tbUserhomework.getHomeworkid();
			String hid[] = homeworkid.split("_");
			String imgname = tbUser.getUsername() + "_作业" + hid[1];			
			Byte flag = tbDisplay.getClassify();
			if(flag == 1){
				DisplayDTO excellenthomework = new DisplayDTO();
				excellenthomework.setAddress(address);
				excellenthomework.setClassify(flag);
				excellenthomework.setImgname(imgname);
				excellenthomework.setImgaddr(tbDisplay.getImgaddr());
				displayDTOList.add(excellenthomework);
			}
			else if(flag == 0){
				DisplayDTO worsehomework = new DisplayDTO();
				worsehomework.setAddress(address);
				worsehomework.setClassify(flag);
				worsehomework.setImgname(imgname);
				worsehomework.setImgaddr(tbDisplay.getImgaddr());
				displayDTOList.add(worsehomework);
			}
		}
		return displayDTOList;
	}
}
